﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common.Extension;
using Common.Model;
using Common.util;
using Dapper;

namespace Common.Dao
{
    public class RtnCloseReasonDao
    {
        /// <summary>
        /// 批量存储历史涨停原因，回补数据
        /// </summary>
        /// <param name="list"></param>
        public static void SaveReason(List<RtnCloseReason> list)
        {
            var sql =
               @"insert ignore rtn_close_reason(stock_code,title,keyword,keywords,detail,type,link,site_time,crawling_time) values (@Stkcode,@Title,@Keyword,@Keywords,@Detial,@Type,@Link,@SiteTime,@CrawlingTime)";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                conn.InsertMultiple(sql, list);
            }
        }

        public static List<RtnCloseReason> GetTodayReason()
        {
            DateTime today = DateTime.Today;
            DateTime tomorrow = DateTime.Today.AddDays(-1);

            var sql =
                "select stock_code AS Stkcode,title AS Title,keyword AS Keyword," +
                "keywords AS Keywords,detail AS Detial,type AS Type," +
                "site_time AS SiteTime,crawling_time AS CrawlingTime " +
                "FROM rtn_close_reason where type=1 and @Tomorrow > site_time and site_time >@Today";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<RtnCloseReason> list = conn.Query<RtnCloseReason>(sql,new
                {
                    Today = today,
                    Tomorrow = tomorrow
                })?.ToList();
                return list;
            }
        }

    }
}